const Controller = require('egg').Controller;

class AnnouncementController extends Controller {
  // 创建公告
  async create() {
    const { ctx, service } = this;

    try {
      // 参数验证
      const rules = {
        admin_id: { type: 'number', required: true },
        title: { type: 'string', required: true },
        content: { type: 'string', required: true }
      };
      ctx.validate(rules);

      const announcementData = ctx.request.body;
      const result = await service.announcement.create(announcementData);

      ctx.body = {
        code: 0,
        data: result,
        msg: '创建成功'
      };
    } catch (error) {
      if (error.errors) {
        ctx.body = {
          code: 1,
          data: null,
          msg: `参数错误: ${error.errors[0].field} ${error.errors[0].message}`
        };
        return;
      }

      ctx.body = {
        code: 1,
        data: null,
        msg: error.message || '创建公告失败'
      };
    }
  }

  // 获取公告列表
  async list() {
    const { ctx, service } = this;

    try {
      const query = ctx.query;
      const result = await service.announcement.getAnnouncements(query);

      ctx.body = {
        code: 0,
        data: result,
        msg: '获取成功'
      };
    } catch (error) {
      ctx.body = {
        code: 1,
        data: null,
        msg: error.message || '获取公告列表失败'
      };
    }
  }

  // 获取公告详情
  async detail() {
    const { ctx, service } = this;

    try {
      const { id } = ctx.params;

      // 验证ID参数
      const rules = {
        id: { type: 'id', required: true }
      };
      ctx.validate(rules, ctx.params);

      const announcement = await service.announcement.getAnnouncementById(id);

      ctx.body = {
        code: 0,
        data: announcement,
        msg: '获取成功'
      };
    } catch (error) {
      if (error.errors) {
        ctx.body = {
          code: 1,
          data: null,
          msg: `参数错误: ${error.errors[0].field} ${error.errors[0].message}`
        };
        return;
      }

      ctx.body = {
        code: 1,
        data: null,
        msg: error.message || '获取公告详情失败'
      };
    }
  }

  // 更新公告信息
  async update() {
    const { ctx, service } = this;

    try {
      const { id } = ctx.params;
      const updateData = ctx.request.body;

      // 验证路径参数
      const paramRules = {
        id: { type: 'id', required: true }
      };
      ctx.validate(paramRules, ctx.params);

      // 验证body参数
      const bodyRules = {
        admin_id: { type: 'number', required: false },
        title: { type: 'string', required: false },
        content: { type: 'string', required: false }
      };
      ctx.validate(bodyRules);

      const result = await service.announcement.updateAnnouncement(id, updateData);

      ctx.body = {
        code: 0,
        data: result,
        msg: '更新成功'
      };
    } catch (error) {
      if (error.errors) {
        ctx.body = {
          code: 1,
          data: null,
          msg: `参数错误: ${error.errors[0].field} ${error.errors[0].message}`
        };
        return;
      }

      ctx.body = {
        code: 1,
        data: null,
        msg: error.message || '更新公告失败'
      };
    }
  }

  // 删除公告
  async delete() {
    const { ctx, service } = this;

    try {
      const { id } = ctx.params;

      // 验证ID参数
      const rules = {
        id: { type: 'id', required: true }
      };
      ctx.validate(rules, ctx.params);

      await service.announcement.deleteAnnouncement(id);

      ctx.body = {
        code: 0,
        data: null,
        msg: '删除成功'
      };
    } catch (error) {
      if (error.errors) {
        ctx.body = {
          code: 1,
          data: null,
          msg: `参数错误: ${error.errors[0].field} ${error.errors[0].message}`
        };
        return;
      }

      ctx.body = {
        code: 1,
        data: null,
        msg: error.message || '删除公告失败'
      };
    }
  }

  // 获取最新公告
  async latest() {
    const { ctx, service } = this;

    try {
      const { limit = 5 } = ctx.query;
      const announcements = await service.announcement.getLatestAnnouncements(limit);

      ctx.body = {
        code: 0,
        data: announcements,
        msg: '获取成功'
      };
    } catch (error) {
      ctx.body = {
        code: 1,
        data: null,
        msg: error.message || '获取最新公告失败'
      };
    }
  }

  // 获取公告统计
  async stats() {
    const { ctx, service } = this;

    try {
      const stats = await service.announcement.getAnnouncementStats();

      ctx.body = {
        code: 0,
        data: stats,
        msg: '获取成功'
      };
    } catch (error) {
      ctx.body = {
        code: 1,
        data: null,
        msg: error.message || '获取公告统计失败'
      };
    }
  }

  // 批量删除公告
  async batchDelete() {
    const { ctx, service } = this;

    try {
      const rules = {
        ids: { type: 'array', required: true }
      };
      ctx.validate(rules);

      const { ids } = ctx.request.body;

      const results = {
        success: 0,
        failed: 0,
        errors: []
      };

      for (const id of ids) {
        try {
          await service.announcement.deleteAnnouncement(id);
          results.success++;
        } catch (error) {
          results.failed++;
          results.errors.push(`公告ID ${id}: ${error.message}`);
        }
      }

      ctx.body = {
        code: 0,
        data: results,
        msg: `批量删除完成，成功: ${results.success}, 失败: ${results.failed}`
      };
    } catch (error) {
      if (error.errors) {
        ctx.body = {
          code: 1,
          data: null,
          msg: `参数错误: ${error.errors[0].field} ${error.errors[0].message}`
        };
        return;
      }

      ctx.body = {
        code: 1,
        data: null,
        msg: error.message || '批量删除失败'
      };
    }
  }
}

module.exports = AnnouncementController;